package customers.service.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import customers.dal.daointerface.CustomerBasicDAO;
import customers.dal.dataobject.CustomerBasicDO;
import customers.model.CustomerBasic;
import customers.model.CustomerBasicReq;
import customers.model.CustomerBasicResp;
import customers.service.CustomerBasicService;

public class CustomerBasicServiceImpl implements CustomerBasicService {
	
	private final static Logger logger = LoggerFactory.getLogger(CustomerBasicServiceImpl.class);
	
	@Autowired
	private CustomerBasicDAO customerBasicMapper;

	@Override
	public CustomerBasicResp<CustomerBasic> queryCustomerBasicByMemberId(String memberId) {
		
		if(memberId == null)
			new CustomerBasicResp("9001","会员不存在");
	
		CustomerBasicDO customerBasicDO = customerBasicMapper.queryByMemberId(memberId);
		CustomerBasic customerBasic = po2Vo(customerBasicDO);
		CustomerBasicResp<CustomerBasic> res = new CustomerBasicResp<CustomerBasic>();
		res.setBody(customerBasic);
		res.setResultCode("0000");
		res.setResultMsg("查询成功");
		return res;
	}
    
	/**
	 * 添加信息
	 * 注册
	 */
	@Override
	public CustomerBasicResp<CustomerBasic> insertCustomerBasic(CustomerBasic customerBasic) {
		CustomerBasicDO customerBasicDO = new CustomerBasicDO();
		customerBasicDO.setEmail(customerBasic.getEmail());
		customerBasicDO.setHeadImgUrl(customerBasic.getHeadImgUrl());
		customerBasicDO.setMemberId(customerBasic.getMemberId());
		customerBasicDO.setNickName(customerBasic.getNickName());
		customerBasicDO.setPassword(getMd5(customerBasic.getPassword()));
		customerBasicDO.setRegisterSource(customerBasic.getRegisterSource());
		customerBasicDO.setMobile(customerBasic.getMobile());
		customerBasicDO.setUnfrozen(1);
		CustomerBasicResp<CustomerBasic> res = new CustomerBasicResp<>();
		int a = customerBasicMapper.insert(customerBasicDO);
		if(a==1){
			res.setBody(customerBasic);
			res.setResultMsg("添加成功");
		}else{
			res.setResultMsg("添加失败");
		}
		return res;
	}
	
	private CustomerBasic po2Vo(CustomerBasicDO customerBasicDO){
		if(customerBasicDO == null)
			return null;
		CustomerBasic customerBasic = new CustomerBasic();
		customerBasic.setEmail(customerBasicDO.getEmail());
		customerBasic.setHeadImgUrl(customerBasicDO.getHeadImgUrl());
		customerBasic.setMemberId(customerBasicDO.getMemberId());
		customerBasic.setMobile(customerBasicDO.getMobile());
		customerBasic.setNickName(customerBasicDO.getNickName());
		customerBasic.setPassword(customerBasicDO.getPassword());
		customerBasic.setUnfrozen(1);
		customerBasic.setRegisterSource(customerBasicDO.getRegisterSource());
		return customerBasic;
		
	}
	/**
	 * MD5加密
	 * @param plainText
	 * @return
	 */
	private String getMd5(String plainText) {  
		  
	    try {  
		  
          MessageDigest md = MessageDigest.getInstance("MD5");  
              md.update(plainText.getBytes());  
              byte b[] = md.digest();  
   
              int i;  
               
              
              StringBuffer buf = new StringBuffer("");  
          for (int offset = 0; offset < b.length; offset++) {  
              i = b[offset];  
              if (i < 0)  
                  i += 256;  
              if (i < 16)  
                  buf.append("0");  
              buf.append(Integer.toHexString(i));  
          }  
          //32位加密  
         return buf.toString();  
         
         } catch (NoSuchAlgorithmException e) {  
             e.printStackTrace();  
             return null;  
         }  
    
    }  

	@Override
	public CustomerBasicResp<CustomerBasic> updateByMemberID(CustomerBasic customerBasic) {
		CustomerBasicDO customerBasicDO = new CustomerBasicDO();
		customerBasicDO.setMemberId(customerBasic.getMemberId());
		customerBasicDO.setPassword(customerBasic.getPassword());
		CustomerBasicResp<CustomerBasic> res = new CustomerBasicResp<>();
		int a = customerBasicMapper.updateByMemberId(customerBasicDO);
		if(a==1){
			res.setResultCode("0000");
			res.setResultMsg("修改成功");
		}else{
			res.setResultCode("9999");
			res.setResultMsg("修改失败");
		}
		return res;
	}

	@Override
	public CustomerBasicResp<CustomerBasic> updateUnfrozenByMemberID(CustomerBasic customerBasic) {
		CustomerBasicDO customerBasicDO = new CustomerBasicDO();
		customerBasicDO.setMemberId(customerBasic.getMemberId());
		customerBasicDO.setUnfrozen(customerBasic.getUnfrozen());
		CustomerBasicResp<CustomerBasic> res = new CustomerBasicResp<>();
		int a = customerBasicMapper.updateUnfrozenByMemberId(customerBasicDO);
		if(a==1){
			res.setResultCode("0000");
			res.setResultMsg("修改成功");
		}else{
			res.setResultCode("9999");
			res.setResultMsg("修改失败");
		}
		return res;
	}

	@Override
	public CustomerBasicResp<CustomerBasic> queryByMemberIdAndPassword(CustomerBasicReq<CustomerBasic> customerBasic) {
		String memberId = customerBasic.getBody().getMemberId();
		String password = customerBasic.getBody().getPassword();
		Map map = new HashMap();
		map.put("memberId", memberId);
		map.put("password", password);
		CustomerBasicDO customerBasicDO = customerBasicMapper.queryByMemberIdAndPassword(map);
        CustomerBasicResp<CustomerBasic> res = new CustomerBasicResp<>();
 		if(customerBasicDO!=null){
        	res.setResultCode("0000");
        	res.setResultMsg("登陆成功");
        }else{
        	res.setResultCode("9999");
			res.setResultMsg("登陆失败");
        }
		return res;
	}
	

	

}
